/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.AdjustBill;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class AdjustBillDaoImpl extends BaseDaoImpl implements AdjustBillDao {
	public AdjustBill get(String billCode) {
		String sql = "from com.minierp.model.AdjustBill as obj where obj.billCode = :billCode and obj.enabled=\'Y\' order by obj.id";
		String[] paramNames = new String[]{"billCode"};
		Object[] values = new Object[]{billCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (AdjustBill) list.get(0);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.AdjustBill where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getListForExamine(String searchKey, boolean examined,
			long tsId, UserBean ub, long estManagerId, Date allowStart,
			int startIndex, int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		String examineFlag = "N";
		String orderByStr = " order by b.adjustDate";
		if (examined) {
			examineFlag = "Y";
			orderByStr = " order by b.adjustDate desc,b.billCode desc";
		}

		String sql = "select b,e from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str
				+ " and b.examined=:examined"
				+ " and b.enabled=\'Y\'"
				+ orderByStr;
		Session sess = null;

		List arg17;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg17 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg17;
	}

	public int getListForExamineCount(String searchKey, boolean examined,
			long tsId, UserBean ub, long estManagerId, Date allowStart)
			throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.examined=:examined" + " and b.enabled=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			List list = q.list();
			if (!list.isEmpty()) {
				int arg15 = Integer.parseInt(list.get(0).toString());
				return arg15;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List<?> getListForCancel(String searchKey, long tsId, UserBean ub,
			String orderBy, long estManagerId, Date allowStart, int startIndex,
			int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		String sql = "select b,e from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.enabled=\'Y\'";
		if (orderBy != null && orderBy.equals("ADJUST_DATE_ASC")) {
			sql = sql + " order by b.adjustDate asc,b.billCode asc";
		} else {
			sql = sql + " order by b.adjustDate desc,b.billCode desc";
		}

		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public int getListForCancelCount(String searchKey, long tsId, UserBean ub,
			long estManagerId, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		String sql = "select count(b.id) from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.enabled=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg13 = Integer.parseInt(list.get(0).toString());
				return arg13;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List<?> getList(String relType, long estId, boolean examined,
			UserBean ub) {
		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "from com.minierp.model.AdjustBill b where b.clientId="
				+ estId
				+ " and b.relType=\'"
				+ relType
				+ "\'"
				+ " and b.examined=\'"
				+ examineFlag
				+ "\'"
				+ " and b.enabled=\'Y\'"
				+ EncCommonHelper
						.createTallySetSQLStr("b.tallySetId", ub, true)
				+ " order by b.adjustDate desc,b.billCode desc";
		return this.getHibernateTemplate().find(sql);
	}

	public List<?> getCost(String billType, long billId, boolean examined) {
		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select sum(i.thisCost),b.adjustType from com.minierp.model.AdjustBill b ,com.minierp.model.AdjustBillItem i where b.id=i.billId and b.relType=:relType and i.businessBillId="
				+ billId
				+ " and b.examined=\'"
				+ examineFlag
				+ "\'"
				+ " and b.enabled=\'Y\'" + " group by b.adjustType";
		Session sess = null;

		List arg9;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("relType", billType);
			arg9 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg9;
	}

	public Object[] getStatusValues(long id) {
		String sql = "select b.updateTime,b.enabled,b.examined from com.minierp.model.AdjustBill b where b.id="
				+ id;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 ? (Object[]) list.get(0) : null;
	}

	public int estCorporate(final long fromClientId, final long toClientId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.AdjustBill set clientId="
								+ toClientId
								+ " ,updateTime=:updateTime"
								+ " where clientId=" + fromClientId;
						Query q = session.createQuery(hql);
						q.setTimestamp("updateTime", new Date());
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getCost(long tsId, UserBean ub, Date startDate, Date endDate)
			throws Exception {
		String str = "";
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select sum(b.cost),b.adjustType from com.minierp.model.AdjustBill b where b.enabled=\'Y\' and b.examined=\'Y\' and b.adjustDate>=:startDate and b.adjustDate<:endDate"
				+ str + " group by b.adjustType";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	public List<?> getList(String billType, long billId) {
		String sql = "select b from com.minierp.model.AdjustBill b ,com.minierp.model.AdjustBillItem s where b.id=s.billId and b.relType=:relType and s.businessBillId="
				+ billId + " and b.enabled=\'Y\'" + " order by b.adjustDate";
		Session sess = null;

		List arg7;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("relType", billType);
			arg7 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg7;
	}

	public List<?> getMonthCost(Date startDate, Date endDate, long tsId,
			UserBean ub) throws Exception {
		String str = "";
		if (tsId != 0L) {
			str = str + " and b.tally_set_id=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tally_set_id",
							ub, true);
		}

		String sql = "select b.adjust_type,month(b.adjust_date),sum(b.cost) from adjust_bill b where b.enabled=\'Y\' and b.examined=\'Y\'"
				+ str
				+ " and b.adjust_date>=:startDate"
				+ " and b.adjust_date<:endDate"
				+ " group by month(b.adjust_date),b.adjust_type";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	public List getExamSmsCount(long[] tsIdArr) {
		if (tsIdArr != null && tsIdArr.length != 0) {
			String sql = "select b.billCode from  com.minierp.model.AdjustBill b where b.examSms=\'Y\'"
					+ PageHelper.createIdArrStr(tsIdArr, "b.tallySetId", true);
			return this.getHibernateTemplate().find(sql);
		} else {
			return new ArrayList();
		}
	}

	public List getExamSmsCount() {
		String sql = "select b.billCode from  com.minierp.model.AdjustBill b where b.examSms=\'Y\'";
		return this.getHibernateTemplate().find(sql);
	}

	public int updateExamSms() {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.AdjustBill set examSms=\'N\' ,updateTime=:updateTime where examSms=\'Y\'";
						Query q = session.createQuery(hql);
						q.setTimestamp("updateTime", new Date());
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int getAppListForExamCount(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId) throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*) from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.examined=\'N\'" + " and b.enabled=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				int arg13 = Integer.parseInt(list.get(0).toString());
				return arg13;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List getAppListForExamCost(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId) throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select b.adjustType,sum(b.cost) from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str
				+ " and b.examined=\'N\'"
				+ " and b.enabled=\'Y\'"
				+ " group by b.adjustType";
		Session sess = null;

		List arg12;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			arg12 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg12;
	}

	public List getAppListForExam(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId, int startIndex, int num)
			throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select b,e.estName from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str
				+ " and b.examined=\'N\'"
				+ " and b.enabled=\'Y\'"
				+ " order by b.adjustDate desc,b.billCode desc";
		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public int getAppListForUnExamCount(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId) throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*) from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str + " and b.examined=\'Y\'" + " and b.enabled=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				int arg13 = Integer.parseInt(list.get(0).toString());
				return arg13;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List getAppListForUnExam(String sk, long tsId, UserBean ub,
			Date allowStart, long mngId, int startIndex, int num)
			throws Exception {
		String str = "";
		if (sk != null) {
			sk = sk.trim();
		}

		if (sk != null && !sk.equals("")) {
			str = " and ((b.billCode like :sk)  or (e.estName like :sk))";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.adjustDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select b,e.estName from com.minierp.model.AdjustBill b,com.minierp.model.Establishment e where b.clientId=e.id"
				+ str
				+ " and b.examined=\'Y\'"
				+ " and b.enabled=\'Y\'"
				+ " order by b.examineTime desc";
		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (sk != null && !sk.equals("")) {
				q.setString("sk", "%" + sk + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}
}